package com.mr.cai.main.activity;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.cai.mr.lib.utils.LogInfo;
import com.mr.cai.main.R;
import com.mr.cai.main.activity.base.MrCaiBaseActivity;
import com.mr.cai.main.fragment.MyPageFragment;
import com.mr.cai.main.utils.CommonCoreUtil;
import com.mr.cai.main.utils.PictureHelper;
import com.mr.cai.main.utils.RotateImageViewAware;
import com.mr.cai.main.utils.UniversalImageLoadTool;

import rx.functions.Action1;

/**
 * Created by hai8108 on 16/2/25.
 */
public class MrCaiRecommendActivity extends MrCaiBaseActivity {
    private final static int DESC_MAX_NUM = 140;
    private final static int SELECT_PIC_KITKAT = 0x201;
    private final static int SELECT_PIC = 0x202;
    private int type = MyPageFragment.FROM_RECOMMEND_ITEM;
    private ImageView[] ivArray = new ImageView[4];
    private String[] ivPathArray = new String[4];
    private EditText descEditTxt;
    private TextView descTv;
    public static void launch (Activity activity, int type) {
        Intent intent = new Intent(activity, MrCaiRecommendActivity.class);
        intent.putExtra("type", type);
        activity.startActivity(intent);
    }

    @Override
    public void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recommend_layout);
        type = getIntent().getIntExtra("type", MyPageFragment.FROM_RECOMMEND_ITEM);
        findView();
        initData();
    }
    private int picSelectIndex = 0;
    private View.OnClickListener onClick = new View.OnClickListener() {
        @Override
        public void onClick (View v) {
            picSelectIndex = (int) v.getTag();
            openSystemPic();
        }
    };

    private void findView () {
        descEditTxt = (EditText) findViewById(R.id.mr_cai_description);
        descEditTxt.addTextChangedListener(new DesTextWatcher());
        descEditTxt.setHint(type ==
                MyPageFragment.FROM_RECOMMEND_ITEM ? R.string.mr_cai_recommend_description_txt : R.string
                .mr_cai_cookbook_description_txt);
        descTv = (TextView) findViewById(R.id.desc_length_tv);
        descTv.setText(0+"/"+DESC_MAX_NUM);
        ivArray[0] = (ImageView) findViewById(R.id.iv_first);
        ivArray[1] = (ImageView) findViewById(R.id.iv_second);
        ivArray[2] = (ImageView) findViewById(R.id.iv_third);
        ivArray[3] = (ImageView) findViewById(R.id.iv_fouth);
        for (int i =0; i<4; i++){
            ivArray[i].setTag(i);
            ivArray[i].setOnClickListener(onClick);
        }
        CommonCoreUtil.setClickAction(findViewById(R.id.top_view).findViewById(R.id.pub_top_left)
                , new Action1() {
            @Override
            public void call (Object o) {
                finish();
            }
        });
        ImageView topRight = (ImageView) findViewById(R.id.top_view).findViewById(R.id.public_top_iv_right);
        topRight.setVisibility(View.VISIBLE);
        topRight.setBackgroundResource(R.drawable.mr_cai_appreise_ok);
        CommonCoreUtil.setClickAction(topRight, new Action1() {
            @Override
            public void call (Object o) {
                finish();
            }
        });
        ((TextView) findViewById(R.id.top_view).findViewById(R.id.pub_top_mid)).setText(type ==
                MyPageFragment.FROM_RECOMMEND_ITEM ? R.string.my_sum_recommend_txt : R.string
                .my_sum_cookbooks_txt);
    }

    /**
     * 打开系统图库获取照片
     */
    private void openSystemPic () {
        if (CommonCoreUtil.isAfterVersionCodeKitkat()) {
            CommonCoreUtil.openSystemPicAfterBuildKitcat(this, SELECT_PIC_KITKAT);
        } else {
            CommonCoreUtil.openSystemPicBeforeBuildKitcat(this, SELECT_PIC);
        }

    }

    private void initData () {

    }
    private void updateSelectPic(String filePath){
        ivPathArray[picSelectIndex] = filePath;
        UniversalImageLoadTool.disPlay("file://" + filePath,
                new RotateImageViewAware(ivArray[picSelectIndex], filePath), R.drawable.recommend_plus_bg);
    }
    @Override
    protected void onActivityResult (int requestCode, int resultCode,
                                     Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case SELECT_PIC:
                case SELECT_PIC_KITKAT:
                    Uri imageFileUri = data.getData();
                    if(imageFileUri != null){
                        String filePath = PictureHelper.getPath(MrCaiRecommendActivity.this,
                                imageFileUri);
                        LogInfo.log("haitian", "filePath="+filePath);
                        updateSelectPic(filePath);
                    }
                    break;
            }
        }
    }

    @Override
    protected void onDestroy () {
        super.onDestroy();
        CommonCoreUtil.hideSoftInput(descEditTxt);
    }
    private class DesTextWatcher implements TextWatcher {
        @Override
        public void beforeTextChanged (CharSequence s, int start,
                                       int count, int after) {
        }

        @Override
        public void onTextChanged (CharSequence s, int start,
                                   int before, int count) {
            if (s == null || s.length() == 0 ) return;
            int length = s.length();
            if(length >=DESC_MAX_NUM){
                CommonCoreUtil.showToast(R.string.over_desc_txt);
            } else{
                descTv.setText(length+"/"+DESC_MAX_NUM);
            }
        }

        @Override
        public void afterTextChanged (final Editable s) {
        }
    }
}
